package com.java2007.hotel.dao.impl;

import com.java2007.hotel.dao.UserDao;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/25
 **/
public class UserDaoImpl implements UserDao {
    /**
     * 查询用户信息
     *
     * @param userName
     * @return
     */
    @Override
    public User findUser(String userName) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT user_id userId,user_name userName,user_password userPassword,user_status userStatus,user_phone userPhone,user_email userEmail,user_role userRole,user_create_time userCreateTime,user_update_time userUpdateTime,user_money userMoney FROM t_user WHERE user_name = ?";
        return queryRunner.query(sql, new BeanHandler<>(User.class), userName);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public int addUser(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "INSERT INTO t_user " +
                "(user_name,user_password,user_status,user_phone,user_email,user_role,user_create_time)" +
                "VALUES(?,?,?,?,?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(), sql, user.getUserName(), user.getUserPassword(), user.getUserStatus(), user.getUserPhone(), user.getUserEmail(), user.getUserRole(), user.getUserCreateTime());
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "UPDATE t_user " +
                "SET user_name = ?,user_password = ?,user_status = ?,user_phone = ?,user_email = ?,user_role = ?,user_update_time = ?,user_money = ? " +
                "WHERE user_id = ?";
        return queryRunner.update(JdbcUtils.getConnection(), sql, user.getUserName(), user.getUserPassword(), user.getUserStatus(), user.getUserPhone(), user.getUserEmail(), user.getUserRole(), user.getUserUpdateTime(), user.getUserMoney(), user.getUserId());
    }


}
